package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.object.pojo.ArrearsLog;
import com.tbit.uqbike.object.pojo.ArrearsLogSum;
import com.tbit.uqbike.object.pojo.dto.ArrearsLogDTO;
import com.tbit.uqbike.object.pojo.dto.ArrearsLogDownloadDTO;
import com.tbit.uqbike.object.pojo.vo.ArrearsLogDetailSumVO;
import com.tbit.uqbike.object.pojo.vo.ArrearsLogSumVO;
import com.tbit.common.entity.view.PageResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.ArrearsLogService;
import com.tbit.uqbike.webmanager.util.csv.annotations.AsyncDownload;
import com.tbit.uqbike.webmanager.util.excel.ExcelUtil;
import com.tbit.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 财务统计-欠款统计
 *
 * @Author:BUGTian
 * @DATE: 2022/11/14
 */
@RestController
@RequestMapping("/arrearsLog")
public class ArrearsLogController {
    @Autowired
    private ArrearsLogService arrearsLogService;

    /**
     * 查询欠款统计
     *
     * @param brandId    品牌id
     * @param accountIds 区域id，多个用,隔开
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @return
     */
    @GetMapping("/sumArrears")
    public Result<ArrearsLogSumVO> sumArrears(Integer brandId, String accountIds, Date startTime, Date endTime) {
        Assert.notNulls("参数有误", brandId, accountIds, startTime, endTime);
        ArrearsLogSumVO arrearsLogSumVO = arrearsLogService.sumArrears(brandId, accountIds, startTime, endTime);
        return Result.success(arrearsLogSumVO);
    }

    /**
     * 欠款详情账单下载
     *
     * @param downloadDTO
     * @return
     */
    @AsyncDownload(defaultFileName = "欠款详情账单")
    @GetMapping("/download")
    public Result download(ArrearsLogDownloadDTO downloadDTO) {
        String url = arrearsLogService.download(downloadDTO);
        return Result.success(url);
    }

    /**
     * 导出欠款统计
     *
     * @param brandId    品牌id
     * @param accountIds 区域id，多个用,隔开
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @return
     */
    @GetMapping("/exportSumArrears")
    public Result<Object> exportSumArrears(Integer brandId, String accountIds, Date startTime, Date endTime) {
        Assert.notNulls("参数有误", brandId, accountIds, startTime, endTime);
        ArrearsLogSumVO arrearsLogSumVO = arrearsLogService.sumArrears(brandId, accountIds, startTime, endTime);
        List<ArrearsLogSum> list = arrearsLogSumVO.getList();
        list.add(arrearsLogSumVO.getSum().setAreaName("汇总"));
        ExcelUtil<ArrearsLogSum> excelUtil = new ExcelUtil<>(ArrearsLogSum.class);
        return Result.success(excelUtil.exportExcel(list, "欠款统计").getData());
    }

    /**
     * 分页查询欠款明细
     *
     * @param arrearsLogDTO
     * @return
     */
    @GetMapping("/arrearsLogPage")
    public Result<PageResult<ArrearsLog>> arrearsLogPage(ArrearsLogDTO arrearsLogDTO) {
        arrearsLogDTO.assertParam();
        PageResult<ArrearsLog> pageResult = arrearsLogService.arrearsLogPage(arrearsLogDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询欠款明细统计数据
     *
     * @param arrearsLogDTO
     * @return
     */
    @GetMapping("/arrearsLogDetailSum")
    public Result<ArrearsLogDetailSumVO> arrearsLogDetailSum(ArrearsLogDTO arrearsLogDTO) {
        arrearsLogDTO.assertParamIgnorePage();
        ArrearsLogDetailSumVO arrearsLogDetailSumVO = arrearsLogService.arrearsLogDetailSum(arrearsLogDTO);
        return Result.success(arrearsLogDetailSumVO);
    }


}
